﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;


namespace DTcms.Web.admin.company
{
    public partial class banner_list : Web.UI.ManagePage
    {
        protected int totalCount; //数据总记录数
        protected int page; //当前页码
        protected int pageSize; //每页大小
        protected int company_id; //每页大小

        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.company_id = DTRequest.GetInt("company_id",0);

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                RptBind("id>0" + CombSqlTxt(this.keywords,this.company_id), "id desc");
            }
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            this.txtKeywords.Text = this.keywords;
            BLL.company_albums bll = new BLL.company_albums();
            this.rptList.DataSource = bll.GetList( _strWhere);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            //string pageUrl = Utils.CombUrlTxt("banner_list.aspx", "keywords={0}&page={1}", this.keywords, "__id__");
            //PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords,int _company_id)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
          
            strTemp.Append(" and company_id=" + _company_id);
          
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like  '%" + _keywords + "%'");
                strTemp.Append(" or company_id in(select id from ch_company where title like  '%" + _keywords + "%' or mobile like '%" + _keywords + "%'))");
            }
            
            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("xiangce_page_size", "DTcmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("banner_list.aspx", "keywords={0}&company_id={1}", txtKeywords.Text,this.company_id.ToString()));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("xiangce_page_size", "DTcmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("banner_list.aspx", "keywords={0}&company_id={1}", txtKeywords.Text, this.company_id.ToString()));
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int sucCount = 0;
            int errorCount = 0;
            BLL.company_albums bll = new BLL.company_albums();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("banner_list.aspx", "keywords={0}&company_id={1}", txtKeywords.Text, this.company_id.ToString()), "parent.loadMenuTree");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            BLL.company_albums bll = new BLL.company_albums();
            if (this.company_id > 0) {
                int count = bll.GetCount(" company_id =" + this.company_id);
                if (count >= 3)
                { JscriptMsg("企业店铺最多能提交三张banner图片！", Utils.CombUrlTxt("banner_list.aspx", "keywords={0}&company_id={1}", txtKeywords.Text, this.company_id.ToString()), "parent.loadMenuTree"); }
                else { 
                    Model.company_albums model = new Model.company_albums();

                    model.company_id = this.company_id;
                    model.typeid = 1;
                    model.thumb_path = txtImgUrl.Text;
                    model.original_path = txtImgUrl.Text;
                    model.add_time = DateTime.Now;
                    if (bll.Add(model) > 0)
                    {
                        JscriptMsg("添加banner成功！", Utils.CombUrlTxt("banner_list.aspx", "keywords={0}&company_id={1}", txtKeywords.Text, this.company_id.ToString()), "parent.loadMenuTree");
                    }
                    else
                    {
                        JscriptMsg("添加banner成功失败！", Utils.CombUrlTxt("banner_list.aspx", "keywords={0}&company_id={1}", txtKeywords.Text, this.company_id.ToString()), "parent.loadMenuTree");
                    }
                }
            }
        }
    }
}